package com.skywolf.chem.match.controller;


import com.skywolf.chem.common.enums.ResponseCodeEnum;
import com.skywolf.chem.common.sysconfig.exception.BizException;
import com.skywolf.chem.common.sysconfig.response.IResponseCode;
import com.skywolf.chem.match.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author sj
 * Date on 2021/9/26  14:06
 * @Version 1.0
 */
public abstract class BaseController {

    /**
     * 获取当前请求
     *
     * @return request
     */
    public HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public Long getUserId() {
        String userId = getRequest().getHeader("user_id");
        if (StringUtils.isBlank(userId)) {
            throw new BizException(ResponseCodeEnum.PARAM_ERROR);
        }
        return Long.parseLong(userId);
    }

    /**
     * 获取当前请求
     *
     * @return response
     */
    public HttpServletResponse getResponse() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }

}
